/**
 * 路由处理模块
 */

const Admin_module = require('../db_modules/admin_module');
const config = require('../utils/config');
const jwtUtil = require('../utils/jwtUtils');

module.exports = class Admin_handler extends Admin_module {
  /**
   * 管理员发布通知
   * @param {number} id
   * @param {string} title
   * @param {string}  content
   * @return {object} results
   */
  static async publishMsg(req, res) {
    console.log('publishMsg api is requested');
    const body = req.body;
    const results = await Admin_module.publishmsg(body.id, body.title, body.content);
    if (results.affectedRows === 1) {
      res.send({ state: 1, msg: '发布通知成功' });
    } else if (results.state === 0) {
      res.send(results);
    } else {
      res.cc('发布通知失败');
    }
  }

  /**
   * 获取总监测人数
   * @return {object} results
   */
  static async getMonitNum(req, res) {
    console.log('getmonitnum api id requested');
    const results = await Admin_module.getMonitNumber();
    if (results.length >= 0) {
      res.send({ state: 1, results });
    } else {
      res.cc('查询失败！')
    }
  }

  /**
   * 获取异常情况人数
   * @return {object} results
   */
  static async getAbnormalNum(req, res) {
    console.log('getAbnormalNum api is requested');
    const results = await Admin_module.getAbnormalNumber();
    if (results.length >= 0) {
      res.send({ state: 1, results });
    } else {
      res.cc('查询失败！')
    }
  }

  /**
   * 获取请假人数
   * @return {object} results
   */
  static async getLeaveNum(req, res) {
    console.log('getLeaveNum api id requested');
    const results = await Admin_module.getLeaveNumber();
    if (results.length >= 0) {
      res.send({ state: 1, results });
    } else {
      res.cc('查询失败！')
    }
  }

  /**
   * 获取当日健康报表上报人数
   * @return {object} results
   */
  static async getCurrHealthNum(req, res) {
    console.log('getCurrHealthNum api is requested');
    const results = await Admin_module.getCurrHealthNumber();
    if (results.length >= 0) {
      res.send({ state: 1, results });
    } else {
      res.cc('查询失败！')
    }
  }

  /**
   * 修改教师管理班级
   * @param {number} c_id
   * @param {number} t_id
   * @return {object} 
   */
  static async changeClass(req, res) {
    console.log('changeclass api is requested');
    const body = req.body;
    const results = await Admin_module.changeClasses(body.c_id, body.u_id);
    if (results.affectedRows === 1) {
      res.send({ state: 1, msg: '修改成功' });
    } else if (results.state === 0) {
      res.send(results);
    } else {
      res.cc('修改失败');
    }
  }

  /**
   * 删除教师用户
   * @param {number}  u_id   
   * @return {object} 
   */
  static async deleteUser(req, res) {
    console.log('deleteTeacher api is requested');
    const body = req.body;
    const results = await Admin_module.deleteUsers(body.u_id);
    if (results.affectedRows === 1) {
      res.send({ state: 1, msg: '删除成功' });
    } else {
      if (results.state === 0) {
        res.send(results);
      } else {
        res.cc('删除失败');
      }
    }
  }

  /**
   * 添加教师成员
   * @param {string} username
   * @param {string} address
   * @param {string} sex
   * @param {string} classes
   * @param {string} type
   * @return {object} results
   */
  static async addUser(req, res) {
    console.log('addUser api is requested');
    const body = req.body;
    const results = await Admin_module.addUsers(body.username, body.address, body.sex, body.classes, body.type);
    if (results.affectedRows === 1) {
      res.send({ state: 1, msg: '添加成功' });
    } else {
      if (results.state === 0) {
        res.send(results);
      } else {
        res.cc('添加失败');
      }
    }
  }

  /**
   * 文件导入学生成员
   * 
   */

  /**
   * 添加班级
   * @param {string} c_id 
   * @param {string} t_id 
   * @param {string} classname 
   * @returns {object}
   */
  static async addClass(req, res) {
    console.log('addClass api is requeset');
    const body = req.body;
    const results = await Admin_module.addClasses(body.c_id, body.t_id, body.classname);
    if (results.affectedRows === 1) {
      res.send({ state: 1, msg: '添加成功' });
    } else {
      if (results.state === 0) {
        res.send(results);
      } else {
        res.cc('添加失败');
      }
    }
  }

  /**
   * 删除班级
   * @param {string} c_id
   * @return {object}
   */
  static async deleteClass(req, res) {
    console.log('deleteClass api is request');
    const body = req.body;
    const results = await Admin_module.deleteClasses(body.c_id);
    if (results.affectedRows === 1) {
      res.send({ state: 1, msg: '删除成功' });
    } else {
      if (results.state === 0) {
        res.send(results);
      } else {
        res.cc('删除失败');
      }
    }
  }

  /**
   * 获取班级列表
   * @returns {Promise <any>}
   */
  static async getClassList(req, res) {
    console.log('getclasslist api is requested');
    const body = req.body;
    const results = await Admin_module.getClasses();
    if (results.length >= 0) {
      res.send({ state: 1, results });
    } else {
      res.cc('获取失败');
    }
  }

  /**
   * 获取班级人员
   * @param {string} c_id 
   * @returns {object}
   */
  static async getClassNum(req, res) {
    console.log('getClassNum api is requested');
    const body = req.body;
    const results = await Admin_module.getClassNumber(body.c_id);
    if (results.length >= 0) {
      res.send({ state: 1, results });
    } else {
      res.cc('获取失败');
    }
  }
}